<?php
include_once 'DataForm/DataForm.php';

class CadastroController extends Nadeb_Controller_Front
{
	
    public function init()
    {
		parent::init();
    }

    public function indexAction()
    {
    	$form = new Site_Model_Signup();
    	$usr = $form->getRegisterUser();
    	
    	$this->view->form = $form->form( $form->getLoggedUserData(), 'full' );
    	$this->view->title = 'Para finalizar a compra informe os dados abaixo.';
    	
    	if( $usr->id ) $this->_redirect(__LINKS__ . 'cadastro/confirm-user');
    }

    public function newUserAction()
    {
    	$form = new Site_Model_Signup();
        $form->typeAccess = $this->_getParam('type');
    	$this->view->form = $form->form( $form->getLoggedUserData(), 'newUser' );
    	$this->view->title = 'Preecha o cadastro para realizar compras em nossa loja.';
    	
    	$this->render('index');
    }
    
    public function confirmUserAction()
    {
    	$form = new Site_Model_Signup();
    	$this->view->form = $form->form( $form->getLoggedUserData(), 'confirm' );
    	$this->view->title = 'Confirme o endereço de entrega e selecione o tipo de entrega.';
    	
    	$this->render('index');
    }
    
    public function saveAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->save( $this->_request->getPost() );
        if( $result['error'] >= 1)
    	{
    		$this->view->form = $form->form( $result['post'], 'full' );
    		$this->view->error = $result['message'][0];
    		$this->view->title = 'Para finalizar a compra informe os dados abaixo.';
    		
    		$this->render('index');
    	}
    	else
    	{
	    	$this->_redirect(__LINKS__ . 'carrinho/send');
    	}
    }
    
    public function saveNewUserAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->saveNewUser( $this->_request->getPost() );
    	
        if( $result['error'] >= 1)
    	{
    		$this->view->form = $form->form( $result['post'], 'newUser' );
    		$this->view->error = $result['message'][0];
    		$this->view->title = 'Preecha o cadastro para realizar compras em nossa loja.';
    		
    		$this->render('index');
    	}
    	else
    	{
	    	$this->_redirect(__LINKS__ . 'cadastro/feedback');
    	}
    }
    
    public function updateConfirmAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->updateConfirmUser( $this->_request->getPost() );
    	if( $result['error'] >= 1)
    	{
    		$this->view->form = $form->form( $result['post'], 'confirm' );
    		$this->view->error = $result['message'][0];
    		$this->view->title = 'Para finalizar a compra informe os dados abaixo.';
    
    		$this->render('index');
    	}
    	else
    	{
    		$this->_redirect(__LINKS__ . 'carrinho/send');
    	}
    }
    
    public function feedbackAction()
    {
    }
    
    public function logoutAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->logout();
    	$this->_redirect( __LINKS__ );
    }
    
    public function loginAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->login( $this->_request->getPost() );
    	
    	if( $result['error'] >= 1)
    	{
    		$this->view->form = $form->form( $result['post'], 'full' );
    		$this->view->error = $result['message'];
    		
    		$this->render('index');
    	}
    	else
    	{
	    	$this->_redirect(__LINKS__ . 'cadastro/confirm-user');
    	}
    }
    
    public function loginNewUserAction()
    {
    	$form = new Site_Model_Signup();
    	$result = $form->login( $this->_request->getPost() );
    	
    	if( $result['error'] >= 1)
    	{
    		$this->view->form = $form->form( $result['post'], 'newUser' );
    		$this->view->error = $result['message'];
    		
    		$this->render('index');
    	}
    	else
    	{
	    	$this->_redirect(__LINKS__);
    	}
    }
    
    public function getAddressAction()
    {
		$address = new Site_Model_GetAddress( $this->_getParam('cep') );
		
		$this->view->html = $address->getAddress();
		$this->_helper->layout->disableLayout();
    }
}

